<template>
  <div id="box apps">
    <div class="title">
      <h3>{{ titlereg }}</h3>
    </div>
    <el-carousel arrow="always" height="700px" :autoplay="false">
      <el-carousel-item>
        <section class="regis">
          <div class="container">
            <h1>登录</h1>
            <form>
              <div class="form-group">
                <label for="username">Username:</label>

                <input
                  v-model="user.username"
                  class="username"
                  placeholder="请输入您的用户名"
                  required
                />
              </div>
              <div class="form-group">
                <label for="password">Password:</label>
                <input
                  v-model="user.password"
                  class="password"
                  id="password"
                  placeholder="请输入您的密码"
                  type="password"
                  required
                />
              </div>
              <button type="submit" class="btn" @click="login">Login</button>
            </form>
          </div>
        </section>
      </el-carousel-item>

      <el-carousel-item>
        <section class="regis">
          <div class="container">
            <h1>注册</h1>
            <form>
              <div class="form-group">
                <label for="username">Username:</label>

                <input
                  v-model="reguser.username"
                  class="username"
                  placeholder="请输入您的用户名"
                  required
                />
              </div>
              <div class="form-group">
                <label for="password">Password:</label>
                <input
                  v-model="reguser.password"
                  class="password"
                  id="passwords"
                  placeholder="请输入您的密码"
                  type="password"
                  required
                />
              </div>
              <div class="form-group">
                <label for="password">Password:</label>
                <input
                  v-model="psw"
                  class="password"
                  id="passwordss"
                  placeholder="请再次确认您的密码"
                  style="border: 0; width: 100%"
                  type="password"
                  required
                />
              </div>
              <button type="button" class="btn btn-register" @click="register">
                Register
              </button>
            </form>
          </div>
        </section>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data: function () {
    return {
      user: {
        username: "",
        password: "",
        role:""
      },
      reguser: {
        username: "",
        password: "",
      },
      psw: "",

      el: false,
      loginflg: false,

      titleflg: true,
      titlereg: "",
    };
  },
  methods: {
    login() {
      if (this.user.username == "" || this.user.password == "") {
        this.$message({
          message: "账号或密码错误",
          type: "error",
        });
        return;
      }

      axios({
        url: "http://localhost:3002/competition/user/login",
        method: "post",
        data: this.user,
      }).then((res) => {
        if (res.data.success === true) {
          this.$message({
            message: "登录成功",
            type: "success",
          });
          let userInfo = this.user;
          userInfo.role = res.data.data.role
          userInfo.id = res.data.data.id
          localStorage.setItem("userInfo", JSON.stringify(userInfo));
          // 根据不同的角色跳转到不同界面，实现简单，但不太安全的访问控制
          //TODO 将来去修正
          if(res.data.data.role=="普通用户")
          {
            window.location.href = "home"; // 如果是普通用户，跳转到home
          }else if(res.data.data.role=="管理员"){
            window.location.href= "admin";// 如果是管理员，跳转到admin
          }
        } else {
          // this.$message({
          //   message: res.data.msg,
          //   type: "error",
          // });
          this.$message.error("登录失败");
        }
      });
    },

    register() {
      if (
        this.reguser.password === this.psw &&
        this.reguser.username != "" &&
        this.reguser.password != ""
      ) {
        axios({
          url: "http://localhost:3002/competition/user/reguser",
          method: "post",
          data: this.reguser,
        }).then((res) => {
          if (res.data.success === true) {
            this.$message({
              message: "注册成功",
              type: "success",
            });
            this.reguser = "";
            this.psw = "";
          } else {
            this.$message({
              message: res.data.msg,
              type: "error",
            });
          }
        });
      } else {
        this.$message({
          message: "账号或密码错误",
          type: "error",
        });
      }
    },
  },

  mounted() {
    const dataString = localStorage.getItem("title");
    if (dataString) {
      const data = JSON.parse(dataString);
      this.titlereg = data.title;
    }

    document.body.style.backgroundColor = "transparent"; // 设置 body 背景色为透明
  },
};
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.el-carousel__item[data-v-a49090ce]:nth-child(2n + 1) {
  background-color: rgba(60, 78, 91, 0.5);
}
.el-carousel__item[data-v-a49090ce]:nth-child(2n) {
  background-color: rgba(60, 78, 91, 0.5);
}

body,
html {
  height: 100%;
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../../assets/userBGR.jpg");
  background-size: cover;
  background-position: center;
}

.container {
  text-align: right;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 300px;
  margin: o auto;
}

h1 {
  margin: 0 0 20px 0;
  color: #333;
  text-align: center;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
}

.form-group input {
  width: 100%;
  padding: 8px;
  box-sizing: border-box;
}

.btn {
  display: inline-block;
  padding: 10px 15px;
  margin-top: 10px;
  color: white;
  background-color: #007bff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  text-align: center;
}

.btn-register {
  background-color: #28a745;
}
.regis {
  position: absolute;
  top: 25%;
  left: 40%;
  margin: 0;
  vertical-align: middle;
}
.title {
  color: #fff;
  margin: 0;
  margin-bottom: 25px;
  letter-spacing: 3px;
}
</style>